﻿using ProxyPattern.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyPattern
{

    public class Person : IPerson
    {
        /// <summary>
        /// 获取用户进度
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public int GetUesrProgress(int year)
        {
            Console.WriteLine($"原生方法，{year}年，得到一辆宝马");
            return year + 1;
        }
    }




    /// <summary>
    /// 代理类
    /// </summary>
    public class ProxyPerson : Person, IPerson
    {
        RealPerson realPerson;

        public int GetUesrProgress(int year)
        {
            if (realPerson == null)
            {
                realPerson = new RealPerson();
            }
            try
            {
                realPerson.Before(year);
                var result = base.GetUesrProgress(year);
                realPerson.After(result);
                Console.WriteLine($"代理方法，{result}年，得到一辆别墅");
                return result;
            }
            catch (Exception e)
            {
                realPerson.Exception(e);
            }
            return default;
        }
    }






    /// <summary>
    /// 代理真实实现类
    /// </summary>
    public class RealPerson
    {

        /// <summary>
        /// 执行前
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public void Before(int year)
        {
            Console.WriteLine($"代理拓展执行前实现，得到参数{year}");
        }

        /// <summary>
        /// 方法执行异常
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public void Exception(Exception e)
        {
            Console.WriteLine($"代理拓展执行异");
            throw e;
        }
        /// <summary>
        /// 执行后
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public void After(int result)
        {
            Console.WriteLine($"代理拓展执行前实现，得到执行结果{result}");
        }
    }
}
